﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing.Imaging;


namespace DigitalScoresheet
{
    public partial class DSMain : Form
    {
        #region Private Application Variables

        private string[] dateTime;
        private string line1 = String.Empty;
        private string line2 = String.Empty;
        private int hGoalCount = 0;
        private int aGoalCount = 0;
        private int hPenaltyCount = 0;
        private int aPenaltyCount = 0;
        private ArrayList buttons = new ArrayList();
        private ArrayList hNumbers = new ArrayList();
        private ArrayList aNumbers = new ArrayList();
        private DataTable PenCountTab = new DataTable();
        private DataTable periodScoringTable = new DataTable();
        private DataTable hRosterTable = new DataTable();
        private DataTable hScoringTable = new DataTable();
        private DataTable hPenaltyTable = new DataTable();
        private DataTable aRosterTable = new DataTable();
        private DataTable aScoringTable = new DataTable();
        private DataTable aPenaltyTable = new DataTable();

        #endregion

        public DSMain()
        {           
            InitializeComponent();
            hRosterGrid.Columns[0].HeaderText = "0";
            aRosterGrid.Columns[0].HeaderText = "0";
            
            //Add Buttons to List and Set Visibility 
            CreateButtonList();
            #region Data Grid and Column Set up

            PenCountTab.Columns.Add("count", typeof(string));
            PenaltyCountGrid.AutoGenerateColumns = false;
            PenaltyCountGrid.DataSource = PenCountTab;

            periodScoringTable.Columns.AddRange(new DataColumn[] { new DataColumn("team", typeof(string)), new DataColumn("period1", typeof(string)), new DataColumn("period2", typeof(string)), new DataColumn("period3", typeof(string)), new DataColumn("period4", typeof(string)), new DataColumn("total", typeof(string)) });
            periodScoringGrid.AutoGenerateColumns = false;
            periodScoringGrid.DataSource = periodScoringTable;


            hRosterTable.Columns.AddRange(new DataColumn[] { new DataColumn("number", typeof(string)), new DataColumn("name", typeof(string)) });
            hRosterGrid.AutoGenerateColumns = false;
            hRosterGrid.DataSource = hRosterTable;

            aRosterTable.Columns.AddRange(new DataColumn[] { new DataColumn("number", typeof(string)), new DataColumn("name", typeof(string)) });
            aRosterGrid.AutoGenerateColumns = false;
            aRosterGrid.DataSource = aRosterTable;

            hScoringTable.Columns.AddRange(new DataColumn[] { new DataColumn("number", typeof(string)), new DataColumn("period", typeof(string)), new DataColumn("time", typeof(string)), new DataColumn("goal", typeof(string)), new DataColumn("assist", typeof(string)), new DataColumn("type", typeof(string)) });
            hScoringGrid.AutoGenerateColumns = false;
            hScoringGrid.DataSource = hScoringTable;

            aScoringTable.Columns.AddRange(new DataColumn[] { new DataColumn("number", typeof(string)), new DataColumn("period", typeof(string)), new DataColumn("time", typeof(string)), new DataColumn("goal", typeof(string)), new DataColumn("assist", typeof(string)), new DataColumn("type", typeof(string)) });
            aScoringGrid.AutoGenerateColumns = false;
            aScoringGrid.DataSource = aScoringTable;

            hPenaltyTable.Columns.AddRange(new DataColumn[] { new DataColumn("period", typeof(string)), new DataColumn("number", typeof(string)), new DataColumn("offense", typeof(string)), new DataColumn("minutes", typeof(string)), new DataColumn("off", typeof(string)), new DataColumn("start", typeof(string)), new DataColumn("on", typeof(string)) });
            hPenaltyGrid.AutoGenerateColumns = false;
            hPenaltyGrid.DataSource = hPenaltyTable;

            aPenaltyTable.Columns.AddRange(new DataColumn[] { new DataColumn("period", typeof(string)), new DataColumn("number", typeof(string)), new DataColumn("offense", typeof(string)), new DataColumn("minutes", typeof(string)), new DataColumn("off", typeof(string)), new DataColumn("start", typeof(string)), new DataColumn("on", typeof(string)) });
            aPenaltyGrid.AutoGenerateColumns = false;
            aPenaltyGrid.DataSource = aPenaltyTable;

            for (int i = 0; i < 16; i++)
            {
                if (i < 15)
                {
                    hPenaltyTable.Rows.Add(new string[] { "", "", "", "", "", "", "" });
                    aPenaltyTable.Rows.Add(new string[] { "", "", "", "", "", "", "" });
                    PenCountTab.Rows.Add((i + 1).ToString());
                }

                hScoringTable.Rows.Add(new string[] { (i + 1).ToString(), "", "", "", "", "" });
                aScoringTable.Rows.Add(new string[] { (i + 1).ToString(), "", "", "", "", "" });
            }  

            #endregion   
        }

        #region Button Click Events

        private void hGoalButton_Click(object sender, EventArgs e)
        {
            
            ButtonVisiblity(false);
            hGoalPanel.Visible = true;
            hGoalPanel.BackColor = Color.Lime;
            if (hGoalCount != 0)
                hPerList.SelectedItem = hScoringTable.Rows[hGoalCount][1];
            else
                hPerList.SelectedItem = hPerList.Items[0];
            hTimeBox.Clear();
            hGoalList.SelectedItem = hGoalList.Items[0];
            hAssistList1.SelectedItem = hAssistList1.Items[0];
            hAssistList2.SelectedItem = hAssistList2.Items[0];
            hGoalType.SelectedItem = hGoalType.Items[0];
            
        }

        private void aGoalButton_Click(object sender, EventArgs e)
        {
            ButtonVisiblity(false);
            aGoalPanel.Visible = true;
            aGoalPanel.BackColor = Color.Lime;
            if (aGoalCount != 0)
                aPerList.SelectedItem = aScoringTable.Rows[aGoalCount][1];
            else
                aPerList.SelectedItem = aPerList.Items[0];
            aTimeBox.Clear();
            aGoalList.SelectedItem = aGoalList.Items[0];
            aAssistList1.SelectedItem = aAssistList1.Items[0];
            aAssistList2.SelectedItem = aAssistList2.Items[0];
            aGoalType.SelectedItem = aGoalType.Items[0];

        }

        private void hEditGoalButton_Click(object sender, EventArgs e)
        {
            
            if (hGoalCount > 0)
            {
                ButtonVisiblity(false);
                hSelectGoalPanel.Visible = true;
            }

        }

        private void aEditGoalButton_Click(object sender, EventArgs e)
        {
            if (aGoalCount > 0)
            {
                ButtonVisiblity(false);
                aSelectGoalPanel.Visible = true;
            }
        }
       
        private void hGoalCancelButton_Click(object sender, EventArgs e)
        {
            hGoalPanel.Visible = false;
            ButtonVisiblity(true);
        }

        private void aGoalCancelButton_Click(object sender, EventArgs e)
        {
            aGoalPanel.Visible = false;
            ButtonVisiblity(true);
        }

        private void hPenBut_Click(object sender, EventArgs e)
        {
           
            ButtonVisiblity(false);
            hPenaltyPanel.BackColor = Color.Red;           
            hPenaltyPanel.Visible = true;
            if (hPenaltyCount != 0)
                hPenPerList.SelectedItem = hPenaltyTable.Rows[hPenaltyCount][0];
            else
                hPenPerList.SelectedItem = hPenPerList.Items[0];
            hPenNumList.SelectedItem = hPenNumList.Items[0];
            hPenOffenseList.SelectedItem = hPenOffenseList.Items[0];
            hPenMinList.SelectedItem = hPenMinList.Items[0];
            hPenOffBox.Clear();
            hPenStartBox.Clear();
            hPenOnBox.Clear();
        }

        private void aPenaltyButton_Click(object sender, EventArgs e)
        {
            ButtonVisiblity(false);
            aPenaltyPanel.BackColor = Color.Red;
            aPenaltyPanel.Visible = true;
            if (aPenaltyCount != 0)
                aPenPerList.SelectedItem = aPenaltyTable.Rows[aPenaltyCount][0];
            else
                aPenPerList.SelectedItem = aPenPerList.Items[0];
            aPenNumList.SelectedItem = aPenNumList.Items[0];
            aPenOffenseList.SelectedItem = aPenOffenseList.Items[0];
            aPenMinList.SelectedItem = aPenMinList.Items[0];
            aPenOffBox.Clear();
            aPenStartBox.Clear();
            aPenOnBox.Clear();
        }

        private void hPenAddBut_Click(object sender, EventArgs e)
        {
            ButtonVisiblity(true);
            hPenaltyPanel.Visible = false;
            hPenaltyTable.Rows[hPenaltyCount].ItemArray = new string[] { hPenPerList.SelectedItem.ToString(), hPenNumList.SelectedItem.ToString(), hPenOffenseList.SelectedItem.ToString(), hPenMinList.SelectedItem.ToString(), FormatTime(hPenOffBox.Text), FormatTime(hPenStartBox.Text), FormatTime(hPenOnBox.Text) };
            hPenaltyCount++;
            hPenSelecter.Items.Add(hPenaltyCount.ToString());
        }

        private void aPenAddBut_Click(object sender, EventArgs e)
        {
            ButtonVisiblity(true);
            aPenaltyPanel.Visible = false;
            aPenaltyTable.Rows[aPenaltyCount].ItemArray = new string[] { aPenPerList.SelectedItem.ToString(), aPenNumList.SelectedItem.ToString(), aPenOffenseList.SelectedItem.ToString(), aPenMinList.SelectedItem.ToString(), FormatTime(aPenOffBox.Text), FormatTime(aPenStartBox.Text), FormatTime(aPenOnBox.Text) };
            aPenaltyCount++;
            aPenSelecter.Items.Add(aPenaltyCount.ToString());
        }

        private void hPenCancelBut_Click(object sender, EventArgs e)
        {
            hPenUpdateBut.Visible = false;
            hPenAddBut.Visible = true;
            ButtonVisiblity(true);
            hPenaltyPanel.Visible = false;

        }

        private void aPenCancelBut_Click(object sender, EventArgs e)
        {
            aPenUpdateBut.Visible = false;
            aPenAddBut.Visible = true;
            ButtonVisiblity(true);
            aPenaltyPanel.Visible = false;
        }

        private void hUpdateGoalBut_Click(object sender, EventArgs e)
        {
            ButtonVisiblity(true);
            hGoalPanel.Visible = false;
            hScoringTable.Rows[hGoalSelecter.SelectedIndex].ItemArray = new string[] { (hGoalSelecter.SelectedIndex + 1).ToString(), hPerList.SelectedItem.ToString(), FormatTime(hTimeBox.Text), hGoalList.SelectedItem.ToString(), hAssistList1.SelectedItem.ToString() + ", " + hAssistList2.SelectedItem.ToString(), hGoalType.SelectedItem.ToString() };
            hUpdateGoalBut.Visible = false;
            hGoalAddBut.Visible = true;

        }

        private void aUpdateGoalButton_Click(object sender, EventArgs e)
        {
            ButtonVisiblity(true);
            aGoalPanel.Visible = false;
            aScoringTable.Rows[aGoalSelecter.SelectedIndex].ItemArray = new string[] { (aGoalSelecter.SelectedIndex + 1).ToString(), aPerList.SelectedItem.ToString(), FormatTime(aTimeBox.Text), aGoalList.SelectedItem.ToString(), aAssistList1.SelectedItem.ToString() + ", " + aAssistList2.SelectedItem.ToString(), aGoalType.SelectedItem.ToString() };
            aUpdateGoalButton.Visible = false;
            aGoalAddButton.Visible = true;
        }

        private void hGoalAddBut_Click(object sender, EventArgs e)
        {
            ButtonVisiblity(true);
            hGoalPanel.Visible = false;
            hScoringTable.Rows[hGoalCount].ItemArray = new string[] { (hGoalCount + 1).ToString(), hPerList.SelectedItem.ToString(), FormatTime(hTimeBox.Text), hGoalList.SelectedItem.ToString(), hAssistList1.SelectedItem.ToString() + ", " + hAssistList2.SelectedItem.ToString(), hGoalType.SelectedItem.ToString() };
            hGoalCount++;
            hGoalSelecter.Items.Add(hGoalCount.ToString());

        }

        private void aGoalAddButton_Click(object sender, EventArgs e)
        {
            ButtonVisiblity(true);
            aGoalPanel.Visible = false;
            aScoringTable.Rows[aGoalCount].ItemArray = new string[] { (aGoalCount + 1).ToString(), aPerList.SelectedItem.ToString(), FormatTime(aTimeBox.Text), aGoalList.SelectedItem.ToString(), aAssistList1.SelectedItem.ToString() + ", " + aAssistList2.SelectedItem.ToString(), aGoalType.SelectedItem.ToString() };
            aGoalCount++;
            aGoalSelecter.Items.Add(aGoalCount.ToString());
        }

        private void hEditPenBut_Click(object sender, EventArgs e)
        {

            if (hPenaltyCount > 0)
            {
                ButtonVisiblity(false);
                hSelectPenaltyPanel.Visible = true;
            }

        }

        private void aEditPenaltyButton_Click(object sender, EventArgs e)
        {
            if (aPenaltyCount > 0)
            {
                ButtonVisiblity(false);
                aSelectPenaltyPanel.Visible = true;
            }
        }

        private void hPenUpdateBut_Click(object sender, EventArgs e)
        {
            ButtonVisiblity(true);
            hPenaltyPanel.Visible = false;
            hPenaltyTable.Rows[hPenSelecter.SelectedIndex].ItemArray = new string[] { hPenPerList.SelectedItem.ToString(), hPenNumList.SelectedItem.ToString(), hPenOffenseList.SelectedItem.ToString(), hPenMinList.SelectedItem.ToString(), FormatTime(hPenOffBox.Text), FormatTime(hPenStartBox.Text), FormatTime(hPenOnBox.Text) };
            hPenUpdateBut.Visible = false;
            hPenAddBut.Visible = true;

        }

        private void aPenUpdateBut_Click(object sender, EventArgs e)
        {
            ButtonVisiblity(true);
            aPenaltyPanel.Visible = false;
            aPenaltyTable.Rows[aPenSelecter.SelectedIndex].ItemArray = new string[] { aPenPerList.SelectedItem.ToString(), aPenNumList.SelectedItem.ToString(), aPenOffenseList.SelectedItem.ToString(), aPenMinList.SelectedItem.ToString(), FormatTime(aPenOffBox.Text), FormatTime(aPenStartBox.Text), FormatTime(aPenOnBox.Text) };
            aPenUpdateBut.Visible = false;
            aPenAddBut.Visible = true;
        }

        private void SubmitBut_Click(object sender, EventArgs e)
        {
            SortColumns();
            SetUpScoringBox();
            ScoreKeeperBox.BorderStyle = BorderStyle.None;
            Ref1Box.BorderStyle = BorderStyle.None;
            Ref2Box.BorderStyle = BorderStyle.None;
            ButtonVisiblity(false);
            this.Refresh();
            Rectangle form = new Rectangle(this.Location.X + 8, this.Location.Y + 30, this.Size.Width - 16, this.Size.Height - 32);
            Bitmap bitmap = new Bitmap(form.Width, form.Height);
            Graphics graphic = Graphics.FromImage(bitmap);
            graphic.CopyFromScreen(form.Location, Point.Empty, form.Size);
            bitmap.Save(string.Format("C:\\Users\\Do Not Enter\\Dropbox\\Scoresheets\\FNML\\April\\({0}) {1} vs. {2}.jpg", dateTime[0].Replace('/', '-'), hTeamLabel.Text, aTeamLabel.Text), ImageFormat.Jpeg);
        }

        private void startButton_Click(object sender, EventArgs e)
        {

            if (leftTeamList.SelectedItem != null && rightTeamList.SelectedItem != null && leftTeamList.SelectedIndex != rightTeamList.SelectedIndex)
            {
                dateTime = dateSelecter.Text.Split(new char[] { ' ' });
                dateLabel.Text = dateTime[0];
                leagueLabel.Text = LeagueList.SelectedItem.ToString();
                startLabel.Text = dateTime[2] + " " + dateTime[3];
                arenaLabel.Text = ArenaList.SelectedItem.ToString();
                surfaceLabel2.Text = SurfaceList.SelectedItem.ToString();
                ButtonVisiblity(true);
                setUpPanel.Visible = false;
                StreamReader leftSR = new StreamReader(@"C:\Users\Do Not Enter\Dropbox\Adult League Stats\" + leftTeamList.SelectedItem.ToString() + ".txt");
                StreamReader rightSR = new StreamReader(@"C:\Users\Do Not Enter\Dropbox\Adult League Stats\" + rightTeamList.SelectedItem.ToString() + ".txt");
                hTeamLabel.Text = leftTeamList.SelectedItem.ToString();
                aTeamLabel.Text = rightTeamList.SelectedItem.ToString();


                while ((line1 = leftSR.ReadLine()) != null)
                {
                    hRosterTable.Rows.Add(line1.Split(new char[] { '\t' }));
                    string[] num = line1.Split(new char[] { '\t' });

                }
                while ((line2 = rightSR.ReadLine()) != null)
                {
                    aRosterTable.Rows.Add(line2.Split(new char[] { '\t' }));
                    string[] num = line2.Split(new char[] { '\t' });

                }

                hRosterGrid.Height = 26 + (22 * hRosterTable.Rows.Count);
                aRosterGrid.Height = 26 + (22 * aRosterTable.Rows.Count);
                periodScoringTable.Rows.Add("", "1", "2", "3", "OT", "Total");

                periodScoringTable.Rows.Add(hTeamLabel.Text, "", "", "", "", "");
                periodScoringTable.Rows.Add(aTeamLabel.Text, "", "", "", "", "");
                DataGridViewCellStyle style = new DataGridViewCellStyle();
                style.Font = new Font(periodScoringGrid.Font, FontStyle.Regular);
                periodScoringGrid.Rows[0].DefaultCellStyle = style;
            }
        }    

        #endregion

        #region TextChanged Events

        private void hPenOffBox_TextChanged(object sender, EventArgs e)
        {
            hPenStartBox.Text = hPenOffBox.Text;
        }

        private void aPenOffBox_TextChanged(object sender, EventArgs e)
        {
            aPenStartBox.Text = aPenOffBox.Text;
        }

        #endregion

        #region SelectedIndex Changed Events

        private void hGoalSelecter_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] Assists = hScoringTable.Rows[hGoalSelecter.SelectedIndex][4].ToString().Split(new char[] { ',' });
            hUpdateGoalBut.Visible = true;
            hGoalAddBut.Visible = false;
            hSelectGoalPanel.Visible = false;
            hGoalPanel.Visible = true;
            hGoalPanel.BackColor = Color.Yellow;
            hPerList.SelectedItem = hScoringTable.Rows[hGoalSelecter.SelectedIndex][1];
            hTimeBox.Text = hScoringTable.Rows[hGoalSelecter.SelectedIndex][2].ToString().Replace(':', '\n');
            hGoalList.SelectedItem = hScoringTable.Rows[hGoalSelecter.SelectedIndex][3];
            hAssistList1.SelectedItem = Assists[0];
            hAssistList2.SelectedItem = Assists[1].Substring(1);
            hGoalType.SelectedItem = hScoringTable.Rows[hGoalSelecter.SelectedIndex][5];
        }

        private void aGoalSelecter_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] Assists = aScoringTable.Rows[aGoalSelecter.SelectedIndex][4].ToString().Split(new char[] { ',' });
            aUpdateGoalButton.Visible = true;
            aGoalAddButton.Visible = false;
            aSelectGoalPanel.Visible = false;
            aGoalPanel.Visible = true;
            aGoalPanel.BackColor = Color.Yellow;
            aPerList.SelectedItem = aScoringTable.Rows[aGoalSelecter.SelectedIndex][1];
            aTimeBox.Text = aScoringTable.Rows[aGoalSelecter.SelectedIndex][2].ToString().Replace(':', '\n');
            aGoalList.SelectedItem = aScoringTable.Rows[aGoalSelecter.SelectedIndex][3];
            aAssistList1.SelectedItem = Assists[0];
            aAssistList2.SelectedItem = Assists[1].Substring(1);
            aGoalType.SelectedItem = aScoringTable.Rows[aGoalSelecter.SelectedIndex][5];     
        }


        private void hPenSelecter_SelectedIndexChanged(object sender, EventArgs e)
        {
            hPenaltyPanel.BackColor = Color.DarkOrange;
            hSelectPenaltyPanel.Visible = false;
            hPenaltyPanel.Visible = true;
            hPenAddBut.Visible = false;
            hPenUpdateBut.Visible = true;
            hPenPerList.SelectedItem = hPenaltyTable.Rows[hPenSelecter.SelectedIndex][0];
            hPenNumList.SelectedItem = hPenaltyTable.Rows[hPenSelecter.SelectedIndex][1];
            hPenOffenseList.SelectedItem = hPenaltyTable.Rows[hPenSelecter.SelectedIndex][2];
            hPenMinList.SelectedItem = hPenaltyTable.Rows[hPenSelecter.SelectedIndex][3];
            hPenOffBox.Text = hPenaltyTable.Rows[hPenSelecter.SelectedIndex][4].ToString().Replace(':', '\n');
            hPenStartBox.Text = hPenaltyTable.Rows[hPenSelecter.SelectedIndex][5].ToString().Replace(':', '\n');
            hPenOnBox.Text = hPenaltyTable.Rows[hPenSelecter.SelectedIndex][6].ToString().Replace(':', '\n');
        }

        private void aPenSelecter_SelectedIndexChanged(object sender, EventArgs e)
        {
            aPenaltyPanel.BackColor = Color.DarkOrange;
            aSelectPenaltyPanel.Visible = false;
            aPenaltyPanel.Visible = true;
            aPenAddBut.Visible = false;
            aPenUpdateBut.Visible = true;
            aPenPerList.SelectedItem = aPenaltyTable.Rows[aPenSelecter.SelectedIndex][0];
            aPenNumList.SelectedItem = aPenaltyTable.Rows[aPenSelecter.SelectedIndex][1];
            aPenOffenseList.SelectedItem = aPenaltyTable.Rows[aPenSelecter.SelectedIndex][2];
            aPenMinList.SelectedItem = aPenaltyTable.Rows[aPenSelecter.SelectedIndex][3];
            aPenOffBox.Text = aPenaltyTable.Rows[aPenSelecter.SelectedIndex][4].ToString().Replace(':', '\n');
            aPenStartBox.Text = aPenaltyTable.Rows[aPenSelecter.SelectedIndex][5].ToString().Replace(':', '\n');
            aPenOnBox.Text = aPenaltyTable.Rows[aPenSelecter.SelectedIndex][6].ToString().Replace(':', '\n');
        }

        private void ArenaList_SelectedIndexChanged(object sender, EventArgs e)
        {
            surfaceLabel.Visible = true;
            SurfaceList.Visible = true;
        }

        private void SurfaceList_SelectedIndexChanged(object sender, EventArgs e)
        {
            rightTeamLabel.Visible = true;
            leftTeamLabel.Visible = true;
            leftTeamList.Visible = true;
            rightTeamList.Visible = true;
            vsLabel.Visible = true;
            startButton.Visible = true;
            if (SurfaceList.SelectedIndex == 0)
            {
                rightTeamLabel.Text = "Home Team";
                leftTeamLabel.Text = "Away Team";
            }
            if (SurfaceList.SelectedIndex == 1)
            {
                rightTeamLabel.Text = "Away Team";
                leftTeamLabel.Text = "Home Team";
            }
        }

        #endregion

        #region Class Methods

            private void ButtonVisiblity(bool b)
            {
                if (b)
                {
                    foreach (Button but in buttons)
                    {
                        but.Visible = true;
                    }
                    //hRosterGrid.ReadOnly = false;
                }
                else
                {
                    foreach (Button but in buttons)
                    {
                        but.Visible = false;
                    }
                   // hRosterGrid.ReadOnly = true;
                }
            }

           private void CreateButtonList()
           {
               foreach (Control ctrl in Controls)
               {
                   if (ctrl is Button)
                   {
                       buttons.Add(((Button)(ctrl)));
                       ((Button)(ctrl)).Visible = false;
                   }
               }
           }

           public string FormatTime(string s)
           {

               if (s.Contains('\n'))
               {
                   string[] temp = s.Split('\n');
                   s = string.Concat(temp[0], temp[1]);
               }

               switch (s.Length)
               {
                   case 1:
                       return "0:0" + s;
                   case 2:
                       return "0:" + s;
                   case 3:
                       return s.Substring(0, 1) + ":" + s.Substring(1, 2);
                   case 4:
                       return s.Substring(0, 2) + ":" + s.Substring(2, 2);
                   case 5:
                       return s.Substring(0, 2) + ":" + s.Substring(3, 2);
               }
               return s;
           }

           private void SortColumns()
           {
               int count = 0;
               DataTable hTempTable = new DataTable();
               DataTable aTempTable = new DataTable();
               hTempTable.Columns.AddRange(new DataColumn[] { new DataColumn("number", typeof(string)), new DataColumn("name", typeof(string)) });
               aTempTable.Columns.AddRange(new DataColumn[] { new DataColumn("number", typeof(string)), new DataColumn("name", typeof(string)) });
               for (int i = 0; i < hRosterGrid.Rows.Count; i++)
               {
                   if ((bool)hRosterGrid[0, i].FormattedValue == true)
                   {
                       hTempTable.Rows.Add(hRosterTable.Rows[i].ItemArray);
                   }
               }
               for (int i = 0; i < hRosterGrid.Rows.Count; i++)
               {
                   if ((bool)hRosterGrid[0, i].FormattedValue == false)
                   {
                       hTempTable.Rows.Add(hRosterTable.Rows[i].ItemArray);
                       count++;
                   }
               }
               hRosterGrid.DataSource = hTempTable;
               for (int i = hRosterGrid.Rows.Count - count; i < hRosterGrid.Rows.Count; i++)
               {
                   hRosterGrid.Rows[i].DefaultCellStyle.ForeColor = Color.Gray;
               }
               count = 0;
               for (int i = 0; i < aRosterGrid.Rows.Count; i++)
               {
                   if ((bool)aRosterGrid[0, i].FormattedValue == true)
                   {
                       aTempTable.Rows.Add(aRosterTable.Rows[i].ItemArray);
                   }
               }
               for (int i = 0; i < aRosterGrid.Rows.Count; i++)
               {
                   if ((bool)aRosterGrid[0, i].FormattedValue == false)
                   {
                       aTempTable.Rows.Add(aRosterTable.Rows[i].ItemArray);
                       count++;
                   }
               }
               aRosterGrid.DataSource = aTempTable;
               for (int i = aRosterGrid.Rows.Count - count; i < aRosterGrid.Rows.Count; i++)
               {
                   aRosterGrid.Rows[i].DefaultCellStyle.ForeColor = Color.Gray;
               }
               hRosterGrid.Columns[0].Visible = false;
               aRosterGrid.Columns[0].Visible = false;
           }

           private void SetUpScoringBox()
           {
               string[] temp = new string[16];
               string[] temp2 = new string[16];
               for (int i = 0; i < 16; i++)
               {
                   temp[i] = hScoringTable.Rows[i].ItemArray[1].ToString();
                   temp2[i] = aScoringTable.Rows[i].ItemArray[1].ToString();
               }
               periodScoringTable.Rows[1].ItemArray = new string[] { hTeamLabel.Text, temp.Count(s => s == "1").ToString(), temp.Count(s => s == "2").ToString(), temp.Count(s => s == "3").ToString(), temp.Count(s => s == "OT" || s == "SO").ToString(), hGoalCount.ToString() };
               periodScoringTable.Rows[2].ItemArray = new string[] { aTeamLabel.Text, temp2.Count(s => s == "1").ToString(), temp2.Count(s => s == "2").ToString(), temp2.Count(s => s == "3").ToString(), temp2.Count(s => s == "OT" || s == "SO").ToString(), aGoalCount.ToString() };
           }  

        #endregion

        #region CellContent Click Events

        private void hRosterGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           hRosterGrid.EndEdit();
           int count = 0;
           hNumbers.Clear();
           hNumbers.Add("--");
           for (int i = 0; i < hRosterTable.Rows.Count; i++)
           {
               if ((bool)hRosterGrid[0, i].FormattedValue == true)
               {
                   hNumbers.Add(hRosterTable.Rows[i][0].ToString());
                   count++;
               }
           }
           hNumbers.Add("X");
           hRosterGrid.Columns[0].HeaderText = count.ToString();
           hGoalList.Items.Clear();
           hGoalList.Items.AddRange(hNumbers.ToArray());
           hGoalList.SelectedItem = "--";
           hAssistList1.Items.Clear();
           hAssistList1.Items.AddRange(hNumbers.ToArray());
           hAssistList1.SelectedItem = "--";
           hAssistList2.Items.Clear();
           hAssistList2.Items.AddRange(hNumbers.ToArray());
           hAssistList2.SelectedItem = "--";
           hPenNumList.Items.Clear();
           hPenNumList.Items.AddRange(hNumbers.ToArray());
           hPenNumList.SelectedItem = "--";
        }

       private void aRosterGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
       {
           aRosterGrid.EndEdit();
           int count = 0;
           aNumbers.Clear();
           aNumbers.Add("--");
           for (int i = 0; i < aRosterTable.Rows.Count; i++)
           {
               if ((bool)aRosterGrid[0, i].FormattedValue == true)
               {
                   aNumbers.Add(aRosterTable.Rows[i][0].ToString());
                   count++;

               }

           }
           aNumbers.Add("X");
           aRosterGrid.Columns[0].HeaderText = count.ToString();
           aGoalList.Items.Clear();
           aGoalList.Items.AddRange(aNumbers.ToArray());
           aGoalList.SelectedItem = "--";
           aAssistList1.Items.Clear();
           aAssistList1.Items.AddRange(aNumbers.ToArray());
           aAssistList1.SelectedItem = "--";
           aAssistList2.Items.Clear();
           aAssistList2.Items.AddRange(aNumbers.ToArray());
           aAssistList2.SelectedItem = "--";
           aPenNumList.Items.Clear();
           aPenNumList.Items.AddRange(aNumbers.ToArray());
           aPenNumList.SelectedItem = "--";
       }

        #endregion

    }
}
